package com.bsierp.util.tags;

import java.util.List;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;


import com.bsierp.business.pojo.Education;
import com.bsierp.business.pojo.Employee;
import com.bsierp.util.Constants;
import com.bsierp.util.hibernate.HibernateSessionFactory;

public class DisplayEducationHtmlSelect extends TagSupport {
	String id;
	public String getId() {
		  return id;
		 }
		 public void setId(String id) {
		  this.id = id;
		 }
		 String usetype;
		 
			public String getUsetype() {
			return usetype;
		}
		public void setUsetype(String usetype) {
			this.usetype = usetype;
		}
	
private Log log = LogFactory.getLog(this.getClass().getName());
   
public int doEndTag( ) throws JspException
{
	
    JspWriter out = pageContext.getOut();
    
    String hql="from Education";
 
try{
	HttpSession ses = pageContext.getSession();
	boolean isedit=false;
	Employee em = null;
	if(this.getUsetype().equals("edit")&&ses.getAttribute(Constants.employee_sessionindex)!=null){
		isedit=true;
	}
	if(isedit==true){
	em=(Employee)ses.getAttribute(Constants.employee_sessionindex);
	System.out.println(em.getEducation().getEducationId());
	}
	
	SessionFactory sessionFactory= HibernateSessionFactory.getSessionFactory();
	Session session=sessionFactory.openSession();
	Transaction tx=null;
	String option_str="";
		try {
		tx=session.beginTransaction();
		List l=session.createQuery(hql).list();
		
		for(int i=0;i<l.size();i++){
			Education pc=(Education)l.get(i);
			
			
			  if(isedit==true){
				   if(em.getEducation().getEducationId().equals(pc.getEducationId())){
				      option_str=option_str+"<option selected=\"selected\" value=\""+pc.getEducationId()+"\">"+pc.getEducationName()+"</option>"   ;
				   }else{
					     option_str=option_str+"<option value=\""+pc.getEducationId()+"\">"+pc.getEducationName()+"</option>"   ;
		 			   }
				   
				   }else{
					   
					   option_str=option_str+"<option value=\""+pc.getEducationId()+"\">"+pc.getEducationName()+"</option>"   ;
			 			  
				   }	
			  }
		
		}catch(Exception e){
			e.printStackTrace();
			
		}finally{
			session.close();
		}
		
		
    out.println("<select name=\"select\" id=\""+this.getId()+"\">" );  
    if(isedit==false){
        out.println("<option value=\"0\" selected=\"selected\">请选择 </option>");
        }
    out.println(option_str);
    out.println("</select>");
	
}catch (Exception ex)
    {
      throw new JspTagException("IOException:" + ex.toString());
    }
    return super.doEndTag();
}
}
